layui.define(['jquery', 'layer'], function (exports) {
    var $ = layui.$,
        layer = layui.layer;
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    var Url = {
        get: function (url, data, options) {
            doRequest(url, 'get', data, options);
        },
        post: function (url, data, options) {
            doRequest(url, 'post', data, options);
        }
    }

    function doRequest(url, type, data, options) {
        var dataType = options.dataType || 'json';
        var success = options.success || function (res) {
        };
        if(options.is_async==1){
            var async = false;
        }else{
            var async = true;
        }
        var fail = options.fail || function (res) {
        };
        var beforeSend = options.beforeSend || function(res){};
        var complete = options.complete || function(res){}

        $.ajax({
            url: url,
            type: type,
            dataType: dataType,
            data: data,
            async:async,
            beforeSend:beforeSend,
            success: success,
            error: fail,
            complete:complete
        });
    }

    /**
     * 错误提示
     *
     * @param errors
     */
    var errorTip = function (errors) {
        var error_text = "<div style='padding:20px'><ul>";
        for (var i in errors) {
            error_text += '<li>' + errors[i] + "</li>";
        }
        error_text += '</ul></div>';
        if (parent) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        Layer.open({
            type: 1,
            title: false,
            content: error_text,
            area: ['400px', '200px'],
            resize: false,
            icon: 5
        })
    }

    $(document).on('click', '.new_tab', function (ev) {
        /**
         * 创建新的Tab
         */
        var icon = $(this).data('icon');
        $(this).find("i").attr('data-icon', icon);
        if (parent && parent.Tab) {
            parent.Tab.tabAdd($(this));
        } else {
            Tab.tabAdd($(this));
        }
        if (ev && ev.preventDefault) {
            ev.preventDefault();
        } else {
            window.event.returnValue = false;
        }
        return false;
    }).on('click', '.javascript', function (ev) {
        /**
         * 需要调用javascript事件处理
         */
        var callback;
        if (callback = $(this).attr('rel')) {
            if(top!=self){
                return false;
            }
            if (window[callback]) {
                window[callback].call(this);
            } else {
                if (callback == 'setAddress') {
                    setAddress(ev.currentTarget);
                }
            }
        }
        if (ev && ev.preventDefault) ev.preventDefault(); else window.event.returnValue = false;
        return false;
    }).on('click', '.J-item-delete', function (ev) {
        /**
         * 删除条目时
         * @href
         * @data-id
         * @data-confirm
         * @data-title
         */
        var url = $(this).prop('href');
        var id = $(this).data('id');
        var confirm = $(this).data('confirm') || '删除后不能恢复，确定要删除吗?';
        var title = $(this).data('title') || '删除确认';

        if (parent.layer) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        Layer.confirm(confirm, {
            title: title,
            resize: false,
            btn: ['立即删除', '再考虑一下'],
            btnAlign: 'c',
            anim: 1,
            icon: 3
        }, function () {
            $.ajax({
                url: url,
                dataType: 'json',
                type: 'post',
                data: {id: id, '_method': 'DELETE'},
                success: function (res) {
                    if (res.code == 1) {
                        Layer.msg('删除成功', {icon: 6, times: 3000}, function () {
                            document.location.reload();
                        })
                    } else {
                        Layer.msg(res.message, {icon: 5, times: 3000});
                    }
                }
            })
        })
        if (ev && ev.preventDefault) ev.preventDefault(); else window.event.returnValue = false;
        return false;
    });
    function setAddress(obj){
        var url = $(obj).attr('href');
        var _latitude =  $(obj).data('latitude') || 'map_latitude';
        var _longitude = $(obj).data('longitude') || 'map_longitude';
        var _address = $(obj).data('address') || 'address';
        var current_document = document;
        if (parent.layer) {
            var Layer = parent.layer;
        } else {
            var Layer = layer;
        }
        Layer.open({
            type: 2,
            title: '地图坐标提取器',
            content: url,
            resize: false,
            area: ['950px', '550px'],
            btn: ['确定修改', '取消'],
            yes: function (index, layero) {
                var contentWindow = $(layero).find('iframe')[0].contentWindow;
                var document = contentWindow.document;
                contentWindow.doSave(function (longitude,latitude,address) {
                    $("#"+_latitude).val(latitude);
                    $("#"+_longitude).val(longitude);
                    $("#"+_address).val(address);
                    Layer.closeAll();
                });
            }
        })
    }
    exports('utils', {
        URL: Url,
        errorTip: errorTip
    })
})